/*
 *  Copyright 2004 Blandware (http://www.blandware.com)
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.blandware.atleap.webapp.action.testimonials;

import com.blandware.atleap.common.TestimonialModuleConstants;
import com.blandware.atleap.model.testimonials.Testimonial;
import com.blandware.atleap.search.SearchManager;
import com.blandware.atleap.service.testimonials.TestimonialManager;
import com.blandware.atleap.webapp.action.core.BaseAction;
import com.blandware.atleap.webapp.form.testimonials.TestimonialForm;
import com.blandware.atleap.webapp.util.core.CacheUtil;
import com.blandware.atleap.webapp.util.core.LinkedObjectsInfoContainer;
import com.blandware.atleap.webapp.util.core.WebappConstants;
import com.blandware.atleap.webapp.util.core.WebappUtil;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>Deletes testimonial
 * </p>
 * <p><a href="DeleteTestimonialAction.java.html"><i>View Source</i></a></p>
 * <p/>
 *
 * @author Andrey Grebnev <a href="mailto:andrey.grebnev@blandware.com">&lt;andrey.grebnev@blandware.com&gt;</a>
 * @version $Revision: 1.12 $ $Date: 2008/04/29 17:52:51 $
 * @struts.action path="/testimonials/deleteTestimonial"
 * name="testimonialForm"
 * scope="request"
 * validate="false"
 * roles="testimonials-item-delete"
 * @struts.action-forward name="listTestimonials"
 * path="/testimonials/list.do"
 * redirect="true"
 * @struts.action-forward name="viewLinkedObjects"
 * path="/core/linkedObjects/view.do"
 * @struts.action-forward name="unsatisfiable"
 * path="/testimonials/list.do"
 */
public final class DeleteTestimonialAction extends BaseAction {
	/**
	 * @param mapping  The ActionMapping used to select this instance
	 * @param form     The optional ActionForm bean for this request (if any)
	 * @param request  The HTTP request we are proceeding
	 * @param response The HTTP response we are creating
	 * @return an ActionForward instance describing where and how
	 *         control should be forwarded, or null if response
	 *         has already been completed
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
	                             HttpServletRequest request, HttpServletResponse response) throws Exception {

		TestimonialForm testimonialForm = (TestimonialForm) form;
		Long testimonialId;
		if ( testimonialForm.getId() != null ) {
			testimonialId = Long.valueOf(testimonialForm.getId());
		} else {
			if ( log.isWarnEnabled() ) {
				log.warn("Missing testimonial item ID. Returning to list...");
			}
			return mapping.findForward("listTestimonials");
		}

		TestimonialManager testimonialManager = (TestimonialManager) getBean(TestimonialModuleConstants.TESTIMONIAL_MANAGER_BEAN);
		Testimonial testimonial = testimonialManager.retrieveTestimonial(testimonialId);

		if ( testimonial == null ) {
			// testimonial not found. it might be deleted by someone else
			ActionMessages errors = new ActionMessages();
			errors.add("testimonialNotFound", new ActionMessage("testimonials.errors.notFound"));
			saveErrors(request, errors);
			return mapping.findForward("listTestimonials");
		}

		if ( !"true".equalsIgnoreCase(request.getParameter("force")) && testimonial.isInUse() ) {
			// trying to delete used testimonial - display warning
            String action = mapping.findForward("listTestimonials").getPath();
            String redirectUrl = WebappUtil.getActionMappingURL(action, null, request, WebappConstants.URL_TYPE_DOMAIN_RELATIVE);
			ActionMessages messages = new ActionMessages();
			messages.add("deleteWarning", new ActionMessage("testimonials.messages.deleteWarning"));
			saveMessages(request, messages);

            LinkedObjectsInfoContainer container = new LinkedObjectsInfoContainer();
            container.setRedirectUrl(redirectUrl);
            container.setDeleteAction(mapping.getPath());
            container.setObjectId(testimonial.getId());
            container.setObjectType(LinkedObjectsInfoContainer.TYPE_PAGE);
            request.getSession().setAttribute(WebappConstants.LINKED_OBJECTS_INFO_CONTAINER_KEY, container);

            saveToken(request);
			return mapping.findForward("viewLinkedObjects");
		}

		String uri = testimonial.getUri();
		testimonialManager.deleteTestimonial(testimonialId);

		//unindex testimonial
		SearchManager searchManager = SearchManager.getInstance(request.getSession().getServletContext());
		searchManager.unIndexPage(uri, request);

		CacheUtil cacheUtil = CacheUtil.getInstance(request);
		cacheUtil.flushLocalizableFieldValueCache(testimonialId);

		return mapping.findForward("listTestimonials");
	}
}